package com.xjnt.mnui.controller;

import java.util.Date;

import com.xjnt.frameworks.annotation.AutoInject;
import com.xjnt.frameworks.annotation.Router;
import com.xjnt.frameworks.core.CRUDController;
import com.xjnt.frameworks.exception.BusinessException;
import com.xjnt.frameworks.web.message.RetMsg;
import com.xjnt.mnui.entity.SubjectAssets;
import com.xjnt.mnui.service.SubjectAssetsService;
import com.xjnt.mnui.service.SubjectService;

@Router(name="/mnui/sub/assets", view="/mnui/subjectAssets")
public class SubjectAssetsController extends CRUDController<SubjectAssets, SubjectAssetsService> {

	@AutoInject
	private SubjectAssetsService subjectAssetsService;
	@AutoInject
	private SubjectService subjectService;
	
	@Override
	protected String getBizName() {
		return "资产";
	}

	@Override
	protected Class<SubjectAssets> getClazz() {
		return SubjectAssets.class;
	}

	@Override
	protected SubjectAssetsService getService() {
		return subjectAssetsService;
	}	
	/**
	 * @Override add方法，增加页面选择专题
	 */
	@Override
	public void add(){
		keepPara();
		addBefore();
		setAttr(getService().getModel().getModelName(), getService().getModel());
		//页面选择专题
		setAttr("subjectList",subjectService.findAll());
		render("add.html");
	}
	/**
	 * @Override edit方法，增加页面选择专题
	 */
	@Override
	public void edit(){
		keepPara();
		SubjectAssets model = getService().findById(getPara("id"));
		editBefore(model);
		setAttr(model.getModelName(), model);
		//页面选择专题
		setAttr("subjectList",subjectService.findAll());
		render("edit.html");
	}
	public void doSave(){
		RetMsg retMsg = new RetMsg();
		try {
			SubjectAssets model = getModel(getClazz(),"subjectAssets");
			model.set("create_time", new Date());
			model.set("type", 0);
			boolean flag = getService().save(model);
			if (flag) {
				retMsg.pushOk("保存成功");
			} else {
				retMsg.pushError("保存失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("保存" + getBizName(), "发生异常：", e);
		}
	}
	
	public void doUpdate(){
		RetMsg retMsg = new RetMsg();
		try {
			SubjectAssets model = getModel(getClazz());
			System.out.println(model.get("sub_uuid"));
			boolean flag = getService().update(model);
			if (flag) {
				retMsg.pushOk("更新成功");
			} else {
				retMsg.pushError("更新失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("更新" + getBizName(), "发生异常：", e);
		}
	}
	/**
	 * 启用/停用 ajax方法
	 */
	public void enableAJax(){
		RetMsg retMsg = new RetMsg();
		String able = getPara("type");//0:启用,1:停用
		String idStr = getPara("ids");
		Object[] ids = idStr.split(",");
		try {
			if("0".equals(able)){//启用操作
				for (Object id : ids) {
					SubjectAssets model = getService().findById(id);
					model.set("type", "0");
					boolean flag = getService().update(model);
					if (flag) {
						retMsg.pushOk("操作成功");
					} else {
						retMsg.pushError("操作失败");
					}
					renderJson(retMsg);
				}
			}else{//停用操作
				boolean type = false;
				for (Object id : ids) {
					SubjectAssets model = getService().findById(id);
					if(model.get("sub_uuid") != null){
						type = true;
						break;
					}
				}
				if(type){
					retMsg.pushError("选择的资产存在所属专题，请确认后操作。");
					renderJson(retMsg);
				}else{
					for (Object id : ids) {
						SubjectAssets model = getService().findById(id);
						model.set("type", 1);
						getService().update(model);
					}
					retMsg.pushOk("批量停用成功");
					renderJson(retMsg);
				}
			}
		} catch (Exception e) {
			throw new BusinessException("保存" + getBizName(), "发生异常：", e);
		}
	}
	/**
	 * 数据删除
	 */
	@Override
	public void deletes() {
		RetMsg retMsg = new RetMsg();
		try {
			String idStr = getPara("ids");
			Object[] ids = idStr.split(",");
			boolean type = false;//删除的记录中是否存在已被专题使用的资产。
			for (Object id : ids) {
				SubjectAssets model = getService().findById(id);
				if(model.get("sub_uuid") != null){
					type = true;
					break;
				}
			}
			if(type){
				retMsg.pushError("删除的数据中存在被专题使用的资产，请确认后操作。");
				renderJson(retMsg);
			}else{
				boolean flag = getService().deleteByIds(ids);
				if (flag) {
					retMsg.pushOk("批量删除成功");
				} else {
					retMsg.pushError("批量删除失败");
				}
				renderJson(retMsg);
			}
		} catch (Exception e) {
			throw new BusinessException("批量删除" + getBizName(), "发生异常：", e);
		}
	}
}
